<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * @package     Pages Module
 * @author      Matrix Infoline (infolinematrix@gmail.com
 * @copyright   (c) Matrix Infoline
 */

class Banner extends Frontend_Controller
{

	public function __construct()
	{
		parent::__construct();
		
		$this->load->library('pagination');
		//$this->admin_commoncall->check_admin_login()--;
		$this->template->set_theme('admin');
		$this->template->set_layout('default');
		$this->load->model('admin/admin_banner_model');
	}
	
	function add(){
	
	
	
		$data['mode'] = "ADD";
		$data['pickup']['banner_size'] = $this->admin_banner_model->getPickup(1);
		
		
		$data['parent']=  $this->category_model->getCetegory(0,9999);
		
		$this->template->set_partial('nav_bar','layouts/nav_bar');
		$this->template->set_partial('side_menu','layouts/side_menu');
		$this->template->build('admin/banner',$data);
		

	}
	
	function edit($id){
	
		$data['mode'] = "EDIT";
	
		$data['banners']['details'] = $this->admin_banner_model->get_banner_details($id);
		$data['banner_size'] = $this->admin_banner_model->getPickup(1);
		$data['parent']=  $this->category_model->getCetegory(0,9999);
	
		$data['id'] = $id;
	
		$this->template->set_partial('nav_bar','layouts/nav_bar');
		$this->template->set_partial('side_menu','layouts/side_menu');
		$this->template->build('admin/banner',$data);
	}
	
	function upload_banner()
	{
	
		if ($this->input->post('upload')) {
	
	
			//upload Photo and thumbnail Starts
			//Form -- TRUE
			// Banner Upload Configuration Start
			$filevariable=array_keys($_FILES);
			$userfile=$filevariable[0];
			$image=$_FILES[$userfile];
	
			$vSize = $this->input->post('banner_size');
			$vSize = explode('X',$vSize);
			$vH=0;
			$vW=0;
	
			$vW=$vSize[0];
			$vH=$vSize[1];
	
	
			$imageData=array(
					'userfile'=>$userfile,
					'imagearray'=>$image,
					'imagepath'=>'./assets/img/advertisement/',
					'allowedtype'=>'jpg|png|gif|jpeg',
					'overwrite'=>'true',
					'removespace'=>'true',
					'customefilename'=>'',     		//do not use extension if not need leave blank
					'namewithtime'=>'yes', 			//time(image_121326.jpg)at last of file name if not need leave blank
					'separator'=>'-', 				//format - image_121326.jpg (.)not allowed, if not require leave blank
					'prefix'=>'yp_',				//format my_image_121326.jpg , if not require leave blank
					'errmessage'=>'',               // If blank default message will shown
					'sucessmessage'=>'',            // If blank default message will shown
					'createthumb'=>'yes',
					'thumbwidth'=>$vW,
					'thumbheight'=>$vH,
					'thumbpath'=>'' 				// Dont write thumb folder name in path
	
			);
			//Banner upload Configure End
			$uploadData=$this->admin_banner_model->upload($imageData);
				
			
			
			$bannerdata=array(
					'value'=>$uploadData['data'],
					'term'=> 'banner',
					'caption' => 'Banner',
					'title' => $this->input->post('banner_title'),
					'size'=>$this->input->post('banner_size'),
					'url'=>$this->input->post('link'),
					//'date_added'=>date('d-m-y',time()),
					'start_date'=>$this->input->post('start_date'),
					'end_date'=>$this->input->post('end_date'),
					'max_impressions'=>$this->input->post('impression'),
					'shows'=>$this->input->post('show'),
					'cat_id' =>$this->input->post('category')
			);
			
		
				
	
	
			if ($uploadData['data'] == TRUE) {
				$this->db->insert('gallery',$bannerdata);
					
					
				$this->session->set_flashdata('message', "<div class='alert alert-success'>You have successful added new banner... </div>");
				redirect('banner/admin/banner/add');
				//}
			}else{
				$this->session->set_flashdata('message', "<div class='alert alert-danger'>Fields can not be null!... </div>");
				redirect('banner/admin/banner/add');
			}
		} else{
	
			$this->session->set_flashdata('message', "<div class='alert alert-danger'>Please Choose a correct file!... </div>");
			redirect('banner/admin/banner/add');
			exit();
		}
	}
	
	
	
	
	
	
	
	function update_banner()
	{
	
		if ($this->input->post('upload')) {
	
	
			// Banner Upload Configuration Start
			$filevariable=array_keys($_FILES);
			$userfile=$filevariable[0];
			$image=$_FILES[$userfile];
	
			$vSize = $this->input->post('banner_size');
			$vSize = explode('X',$vSize);
			$vH=0;
			$vW=0;
	
			$vW=$vSize[0];
			$vH=$vSize[1];
	
	
			$imageData=array(
					'userfile'=>$userfile,
					'imagearray'=>$image,
					'imagepath'=>'./assets/img/advertisement/',
					'allowedtype'=>'jpg|png|gif|jpeg',
					'overwrite'=>'true',
					'removespace'=>'true',
					'customefilename'=>'',     		//do not use extension if not need leave blank
					'namewithtime'=>'yes', 			//time(image_121326.jpg)at last of file name if not need leave blank
					'separator'=>'-', 				//format - image_121326.jpg (.)not allowed, if not require leave blank
					'prefix'=>'yp_',				//format my_image_121326.jpg , if not require leave blank
					'errmessage'=>'',               // If blank default message will shown
					'sucessmessage'=>'',            // If blank default message will shown
					'createthumb'=>'yes',
					'thumbwidth'=>$vW,
					'thumbheight'=>$vH,
					'thumbpath'=>'' 				// Dont write thumb folder name in path
	
			);
			//Banner upload Configure End
			$uploadData=$this->admin_banner_model->upload($imageData);
				
				
				
			$id = $this->input->post('id');
				
			if($uploadData == TRUE){
					
				$bannerdata=array(
						'value'=>$uploadData['data'],
						'term'=>'banner',
						'caption' => 'Banner',
						'title' => $this->input->post('banner_title'),
						'size'=>$this->input->post('banner_size'),
						'url'=>$this->input->post('link'),
						//'date_added'=>date('d-m-y',time()),
						'start_date'=>$this->input->post('start_date'),
						'end_date'=>$this->input->post('end_date'),
						'max_impressions'=>$this->input->post('impression'),
						'shows'=>$this->input->post('show'),
						'cat_id'=>$this->input->post('category')
				);
	
	
				//var_dump($bannerdata);
				
				//--Delete Old Image File--//
	
	
	
				$sql = "Select value from gallery where id=$id and term='banner'";
				$filename = $this->db->query($sql)->row()->value;
				$f = BASEPATH.'../assets/img/advertisement/'.$filename;
				unlink($f);
					
				$ext = preg_replace("/.*\.([^.]+)$/", "\\1", $filename);
				$d =  strpos($filename, '_thumb');
				$n = substr($filename,0,$d);
				$f = BASEPATH.'../assets/img/advertisement/'.$n.'.'.$ext;
			//	var_dump($f);die();
				
				unlink($f);
					
				$this->db->where('id',$id);
				$this->db->update('gallery',$bannerdata);
				$this->session->set_flashdata('message', "<div class='alert alert-success'>Banner Updated Sucessfully!...</div>");
				redirect('banner/admin/banner/edit/'.$this->input->post('id'));
					
			}else {
				$bannerdata=array(
						
						'term'=>'banner',
						'caption' => 'Banner',
						'title' => $this->input->post('banner_title'),
						'size'=>$this->input->post('banner_size'),
						'url'=>$this->input->post('link'),
						//'date_added'=>date('d-m-y',time()),
						'start_date'=>$this->input->post('start_date'),
						'end_date'=>$this->input->post('end_date'),
						'max_impressions'=>$this->input->post('impression'),
						'shows'=>$this->input->post('show'),
						'cat_id'=>$this->input->post('category')
				);
			}
				
				
				
				
			$this->db->where('id',$id);
			$this->db->update('gallery',$bannerdata);
			$this->session->set_flashdata('message', "<div class='alert alert-success'>Banner Updated Sucessfully!...</div>");
			redirect('banner/admin/banner/edit/'.$this->input->post('id'));
				
		} else {
			$this->session->set_flashdata('message', "<div class='alert alert-danger'>Invalid!...</div>");
			redirect('banner/admin/banner/edit/'.$this->input->post('id'));
		}
			
	}
	
	function banner_list()
	{
		//-- Default Value --//
		$status=51;
		$featured=false;
		$search = "";
		$impression=NULL;
		$clicks=NULL;
	
		//$current_datetime=date_default_timezone_set('Africa/Lagos H:m:s');;
		$current_datetime=date('Y-m-d H:m:s');;
		$expire_datetime=24;
		$expire= NULL;
	
		if($this->input->post('filter')){
			$filter = $this->input->post('filter');
	
	
			switch ($filter) {
				case 'active':
					$status = 51;
					break;
				case 'inactive':
					$status = 1;
					break;
				case 'featured':
					$status = 51;
					$featured=true;
					break;
				case 'impressions':
					$status = 51;
					$impression=1;
					//die('hhh');
					break;
	
				case 'clicks':
					$status = 51;
					$clicks=1;
	
	
					break;
	
				case 'expire':
					$status = 51;
					$expire=1;
					break;
	
			}
		}
	
		$data['banners']['info'] = $this->admin_banner_model->getBanners($featured = $featured, $status = $status, $impression=$impression, $clicks=$clicks, $expire=$expire);
	
	
		$this->template->set_partial('nav_bar','layouts/nav_bar');
		$this->template->set_partial('side_menu','layouts/side_menu');
		$this->template->build('admin/banner_list',$data);
	}
	function inactive()
	{
		//-- Default Value --//
		$status=1;
		$featured=false;
		$search = "";
		$impression=NULL;
		$clicks=NULL;
		$expire_datetime=24;
		$expire= NULL;
	
		if($this->input->post('filter')){
			$filter = $this->input->post('filter');
	
	
			switch ($filter) {
				case 'active':
					$status = 51;
					break;
				case 'inactive':
					$status = 1;
					break;
				case 'featured':
					$status = 51;
					$featured=true;
					break;
				case 'impressions':
					$status = 51;
					$impression=1;
					//die('hhh');
					break;
	
				case 'clicks':
					$status = 51;
					$clicks=1;
	
	
					break;
	
				case 'expire':
					$status = 51;
					$expire=1;
					break;
	
			}
		}
	
	
		$status=1;
		$offset = 0 + $this->uri->segment(5);
	
		$total = $this->admin_counter_model->count_inactive_banner($status);
	
		//print "$total";
		//die();
	
		$perpage = 5;
		$config['per_page'] = $perpage;
		$config['total_rows'] = $total;
		$config['uri_segment'] = '5';
	
		$config['base_url'] = base_url() . 'banner/admin/banner/inactive/';
	
		$cur =base_url()."banner/admin/banner/inactive/";
		$cur = '<a href="'. $cur   .'">&laquo; First</a>';
		$config['full_tag_open'] = '<div class="pagination"><ul>';
		$config['full_tag_close'] = '</ul></div><!--pagination-->';
		$config['first_link'] = '&laquo; First';
		$config['first_tag_open'] = '<li class="prev page" style="display:inline">';
		$config['first_tag_close'] = '</li>';
		$config['last_link'] = 'Last &raquo;';
		$config['last_tag_open'] = '<li class="next page" style="display:inline">';
		$config['last_tag_close'] = '</li>';
		$config['next_link'] = 'Next &rarr;';
		$config['next_tag_open'] = '<li class="next page" style="display:inline">';
		$config['next_tag_close'] = '</li>';
		$config['prev_link'] = '&larr; Previous';
		$config['prev_tag_open'] = '<li class="prev page" style="display:inline">';
		$config['prev_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><a href="">';
		$config['cur_tag_close'] = '</a></li>';
		$config['num_tag_open'] = '<li class="page">';
		$config['num_tag_close'] = '</li>';
		$config['display_pages'] = FALSE;
	
		$data['banners']['info'] = $this->admin_banner_model->getInactivebanners($featured = $featured, $status = $status,$perpage, $offset, $impression=$impression, $clicks=$clicks, $expire=$expire);
	
		$this->pagination->initialize($config);
	
	
		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')
		->build('admin/banner_list',$data);
	}
	
	
	
	
	function change_banner_status($status=NULL,$id=NULL) {
		$this->admin_banner_model->change_status($id,$status);
		redirect('banner/admin/banner/banner_list');
	}
	
	
	
	//-- Banner Delete (Subha)--//
	function delete($id,$image_file) {
		$this->admin_banner_model->delete_banner($id,$image_file);
		redirect('banner/admin/banner/banner_list');
	}
	
	
}